package de.listrong.da.ontology.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.eaio.uuid.UUID;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntDocumentManager;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;

import de.listrong.da.ontology.FileSource;
import de.listrong.da.ontology.SentenceIndividual;
import de.listrong.da.ontology.SentenceList;
import de.listrong.da.tools.NewIDGener;
import org.apache.log4j.Logger;

public class AboxWriter {
    public static Logger myLogger = Logger.getLogger(AboxWriter.class);

	private OntModel abox;
	private OntModel tbox;

    private int simpleID;
	
	static public OntModel loadTBox(String filepath, Boolean ifProcessImports) {
		OntModel tbox = ModelFactory.createOntologyModel();
		OntDocumentManager dmgr = tbox.getDocumentManager();
		dmgr.setProcessImports(ifProcessImports);
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader(filepath));
			tbox.read(reader, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tbox;
	}

    /*--- constructor ---*/ 
	public AboxWriter() {

        simpleID = NewIDGener.newId("file");
		this.abox = ModelFactory.createOntologyModel();
		// String aBoxSource = "http://www.example.com/ontologies/abox_"+id.toString();
        String aBoxSource = "http://www.example.com/ontologies/abox_"+ String.valueOf(simpleID);
		String aBoxNS = aBoxSource + "#";
		abox.setNsPrefix("base", aBoxNS);
		abox.setNsPrefix("", aBoxNS);
		this.tbox = loadTBox("e:/ontologies/MedOnto.owl", false);
		String tBoxNS = tbox.getNsPrefixURI("");
		abox.setNsPrefix("medOnt", tBoxNS);
		com.hp.hpl.jena.ontology.Ontology ont = abox.createOntology(abox
				.getNsPrefixURI(""));
		ont.addImport(tbox.createResource("http://www.test.com/MedOnto.owl"));
	}


	/**
	 * a FileSource object will put itself into the A-Box.
	 * @param file - FileSource object
	 */
	public void writeDocumentInstance(FileSource file) {
		file.toOntIndiviual(tbox, abox);
	}

	/**
	 * 
	 * @param sentenceList
	 *            sentenceList is a list of SentenceIndividual
	 */
	public void writeSenttenceInstance(SentenceList sentenceList, String fileSourceID) {
		String tBoxDefaultNS = tbox.getNsPrefixURI("");
		String aBoxDefaultNS = abox.getNsPrefixURI("");
		Individual doc = abox
				.getIndividual(aBoxDefaultNS + "DOC-"+ fileSourceID);
		for (SentenceIndividual sentenceIndividual : sentenceList) {
			Individual ind0 = sentenceIndividual.toOntIndiviual(tbox, abox); 
			ObjectProperty containProperty = tbox
					.getObjectProperty(tBoxDefaultNS + "contains");
			doc.addProperty(containProperty, ind0);
		}
	}

	/* 
	 * there are three options "RDF/XML"(default), "RDF/XML-ABBREV", "N-TRIPLE".
	 */
	public void writeToFile(String outputOrder) {
		BufferedWriter writer1 = null;
		// RDFWriter aBoxWriter = abox.getWriter("RDF/XML-ABBREV");
		try {
            String filename =  "abox_" + this.simpleID + ".owl";
			writer1 = new BufferedWriter( new FileWriter(outputOrder + "\\" + filename) );

            myLogger.info("Write ABox file " +  outputOrder + "\\" + filename );

			// declaration XML preprocessor
			writer1.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			
			// aBoxWriter.setProperty("showXMLDeclaration", "false");
			// aBoxWriter.setProperty("showDoctypeDeclaration", "true");
			// aBoxWriter.write(abox, writer1, "RDF/XML-ABBREV");
			abox.write(writer1, "RDF/XML-ABBREV");
		} catch (IOException e) {
			e.printStackTrace();
            myLogger.error("Error by writing ABox " + e);
		} // end fo try ... catch
        
		// abox.write(System.out, "RDF/XML-ABBREV");
        try {
            writer1.close();
        } catch (Exception e) {
            e.printStackTrace();
            myLogger.error("Error by closing the BufferedWriter " + e);
        }
	}

}
